import 'dart:io';
import 'dart:typed_data';

import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pdzs_app/controllers/controllers.dart';
import 'package:pdzs_app/pages/widgets/menu.dart';
import 'package:pdzs_app/utils/routes.dart';

class HomePage extends StatelessWidget {
  final TokenController _tokenController = Get.find();
  final UploadController _uploadController = Get.put(UploadController());
  static final GlobalKey<ScaffoldState> _globalKey = GlobalKey();
  HomePage({super.key});
  _upload() async {
    // 调用上传凭证接口
    FilePickerResult? result = await FilePicker.platform.pickFiles();

    if (result != null) {
      // ignore: await_only_futures
      // File file = File.fromUri(result.files.single.path as Uri);
      // ignore: unused_local_variable, await_only_futures
      var fileData =
          await File.fromRawPath(result.files.single.path as Uint8List);
      print(fileData);
      _uploadController.uploadFile(fileData);
    } else {
      // User canceled the picker
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        key: _globalKey, //设置key
        drawer: const Menu(),
        appBar: AppBar(
          leading: IconButton(
              onPressed: () => _globalKey.currentState!.openDrawer(),
              icon: const Icon(
                Icons.menu,
                color: Colors.white,
              )),
          title: const Text(
            "首页",
            style: TextStyle(
              color: Colors.white,
            ),
          ),
          centerTitle: true,
          backgroundColor: Colors.blue,
          // 添加一个退出按钮
          actions: [
            IconButton(
              icon: const Icon(
                Icons.logout,
                color: Colors.white,
              ),
              onPressed: () async {
                await _tokenController.logout();
                Get.offAllNamed(Routes.login);
              },
            ),
          ],
        ),
        body: Padding(
          padding: const EdgeInsets.all(20.0), // 设置内边距，使内容与边框有一定的距离
          child: Column(children: [
            const TextField(
              maxLines: 12,
              readOnly: true,
              decoration: InputDecoration(
                border: OutlineInputBorder(),
                hintText: '请上传Token',
              ), //一个搜索图标
            ),
            const SizedBox(height: 16),
            ElevatedButton(
              onPressed: _upload,
              child: const Text('上传Token'),
            )
          ]),
        ));
  }
}
